﻿using System.Collections.Generic;
using MiniJSON;

namespace Framework
{
    public static class JsonHelper
    {
        /// <summary>
        ///可以序列化Dic和数组，不能序列化List
        /// </summary>
        /// <param name="obj">Dic Array</param>
        /// <returns></returns>
        public static string Serialize(object obj)
        {
            if (null == obj)
            {
                Logger.LogError("JsonHelper Serialize, obj is null!");
                return null;
            }
            return Json.Serialize(obj);
        }

        /// <summary>
        /// 反序列化字典
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static Dictionary<string, object> DerializeAsDic(string jsonStr)
        {
            if (string.IsNullOrEmpty(jsonStr))
            {
                Logger.LogError("JsonHelper DerializeAsDic, jsonStr is null or empty!");
                return null;
            }

            return Json.Deserialize(jsonStr) as Dictionary<string, object>;
        }

        /// <summary>
        /// 反序列化数组
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public static List<object> DerializeAsList(string jsonStr)
        {
            if (string.IsNullOrEmpty(jsonStr))
            {
                Logger.LogError("JsonHelper DerializeAsList, jsonStr is null or empty!");
                return null;
            }

            return Json.Deserialize(jsonStr) as List<object>;
        }

        public static int GetJsonInt(object intObj)
        {
            return System.Convert.ToInt32(intObj);
        }

        public static long GetJsonLong(object longObj)
        {
            return System.Convert.ToInt64(longObj);
        }

        public static float GetJsonFloat(object floatObj)
        {
            return System.Convert.ToSingle(floatObj);
        }

        public static bool GetJsonBoolean(object boolObj)
        {
            return System.Convert.ToBoolean(boolObj);
        }

        public static string GetJsonStr(object jsonObj)
        {
            if (null == jsonObj)
            {
                return string.Empty;
            }
            return jsonObj.ToString();
        }

        /// <summary>
        /// 获取json对象的数据
        /// </summary>
        /// <param name="valueObj">json对象</param>
        /// <typeparam name="T">类型</typeparam>
        /// <returns></returns>
        public static object GetJsonVal<T>(object valueObj)
        {
            System.Type type = typeof(T);
            if (type == typeof(string))
            {
                return valueObj;
            }
            if (type == typeof(long))
            {
                return System.Convert.ToInt64(valueObj);
            }
            if (type == typeof(int))
            {
                return System.Convert.ToInt32(valueObj);
            }
            if (type == typeof(float))
            {
                return System.Convert.ToSingle(valueObj);
            }
            if (type == typeof(double))
            {
                return System.Convert.ToDouble(valueObj);
            }
            if (type == typeof(bool))
            {
                return System.Convert.ToBoolean(valueObj);
            }
            return default(T);
        }

        /// <summary>
        /// 获取json数组
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<T> GetJsonList<T>(string jsonStr)
        {
            List<T> ret = new List<T>() { };
            if (null == jsonStr)
            {
                return ret;
            }
            List<T> list = new List<T>();
            List<object> data = (List<object>)Json.Deserialize(jsonStr);
            if (null == list)
            {
                return ret;
            }
            for (int idx = 0; idx < list.Count; idx++)
            {
                list.Add((T)GetJsonVal<T>(data[idx]));
            }
            return list;
        }

        /// <summary>
        /// 从json反序列化得到的字典中取出某个key的值，可能不存在这个key
        /// </summary>
        /// <param name="jsonTab">反序列化得到的字典</param>
        /// <param name="keyName">关键词</param>
        /// <param name="defaultVal">取不到的默认值</param>
        /// <param name="ret">返回值</param>
        /// <typeparam name="T">目标值的类型</typeparam>
        public static void TryGetJsonValue<T>(Dictionary<string, object> jsonTab, string keyName, T defaultVal, out T ret)
        {
            if (jsonTab.ContainsKey(keyName))
            {
                ret = (T)GetJsonVal<T>(jsonTab[keyName]);
            }
            else
            {
                ret = defaultVal;
            }
        }
    }
}
